//
//  LSHomeCell.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/18.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSHomeCell.h"
#import "BKLineChartView.h"
#import "KZShadowButton.h"

@interface LSHomeCell ()

@property (nonatomic,strong) UIImageView *loadingImageView;

@end

@implementation LSHomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        NSMutableArray *images = [NSMutableArray array];
        for (NSInteger i = 0; i<16; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Loading%ld",i+1]];
            [images addObject:image];
        }
        _loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-60)/2,(ScalesH * 260-60)/2 , 60, 60)];
        _loadingImageView.animationImages = images;
        _loadingImageView.animationDuration = 1;
        _loadingImageView.animationRepeatCount = 0;
        [_loadingImageView startAnimating];
        
        [self addSubview:_loadingImageView];
        
        UILabel *line = [[UILabel alloc] init];
        [self addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
        
    }
    return self;
}

- (void)setWeakModel:(weak *)weakModel {
    _weakModel = weakModel;
    [_loadingImageView stopAnimating];
    
    while ([self.subviews lastObject]!=nil) {
        [(UIView *)[self.subviews lastObject] removeFromSuperview];
    }
    
    [self drawLineChart];
}

- (void)drawLineChart {
    
    NSString *ZDTimeString = _weakModel.AbiSocreList.firstObject.ZDTimeString;
    NSString *ZDScoreString = _weakModel.AbiSocreList.firstObject.ZDScoreString;
 
    if([ZDScoreString isEqualToString:@""] || [ZDTimeString isEqualToString:@""]) {
        
        UILabel *textLabel  = [[UILabel alloc] init];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont systemFontOfSize:18.0f];
        [self addSubview:textLabel];
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            make.top.equalTo(self).offset(0);
            make.height.mas_equalTo(40);
        }];
        textLabel.text = [NSString stringWithFormat:@"%@智能分析",_weakModel.AbiName];
        
        UIImageView *noDataImageView = [[UIImageView alloc] init];
        noDataImageView.image = [UIImage imageNamed:@"暂无数据图标"];
        [self addSubview:noDataImageView];
        [noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(78, 83));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"暂无数据,建议先进行听说备考训练";
        label.font = FONT(13);
        label.textColor = RGBA(155, 155, 155, 1);
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(noDataImageView.mas_bottom).offset(12);
            make.size.mas_equalTo(CGSizeMake(kWidth, 13));
        }];
        
        UILabel *line = [[UILabel alloc] init];
        [self addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
        
    } else {
        
        UIView *backView = [[UIView alloc] init];
        [self addSubview:backView];
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        
        UILabel *textLabel  = [[UILabel alloc] init];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont systemFontOfSize:18.0f];
        [backView addSubview:textLabel];
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView).offset(10);
            make.right.equalTo(backView).offset(-10);
            make.top.equalTo(backView).offset(0);
            make.height.mas_equalTo(40);
        }];
        textLabel.text = [NSString stringWithFormat:@"%@智能分析",_weakModel.AbiName];
        
        WeakObj(self);
        KZShadowButton *enterBtn = [[KZShadowButton alloc] init:^(UIButton *button) {

            [button addTarget:selfWeak action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setTitle:[NSString stringWithFormat:@"%@强化训练",selfWeak.weakModel.AbiName] forState:UIControlStateNormal];
            button.titleLabel.font = FONT(14);
            if ([selfWeak.weakModel.AbiClass isEqualToString:@"A"]) { //听力能力
                button.backgroundColor = RGBA(11, 178, 242, 1);
                [button setImage:[UIImage imageNamed:@"听力能力强化训练图标"] forState:UIControlStateNormal];
            } else if ([selfWeak.weakModel.AbiClass isEqualToString:@"B"]) {//发音//朗读
                button.backgroundColor = RGBA(5, 174, 9, 1);
                [button setImage:[UIImage imageNamed:@"听力词汇强化学习图标"] forState:UIControlStateNormal];
            } else if ([selfWeak.weakModel.AbiClass isEqualToString:@"C"]) {//词汇
                button.backgroundColor = RGBA(5, 174, 9, 1);
                [button setImage:[UIImage imageNamed:@"听力词汇强化学习图标"] forState:UIControlStateNormal];
            }
//            button.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 60);
            button.titleEdgeInsets = UIEdgeInsetsMake(0,2, 0, 0);

        }];
        [backView addSubview:enterBtn];
        enterBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(backView);
            make.bottom.equalTo(backView.mas_bottom).offset(-30);
            make.size.mas_equalTo(CGSizeMake(150*ScalesW, 35*ScalesH));
        }];
        
        UILabel *leftLabel = [[UILabel alloc] init];
        [backView addSubview:leftLabel];
        leftLabel.textColor = RGBA(155, 155, 155, 1);
        leftLabel.font = [UIFont systemFontOfSize:10.f];
        leftLabel.text = @"推荐训练方式:";
        leftLabel.textAlignment = NSTextAlignmentRight;
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(enterBtn.mas_left).offset(-3);
            make.centerY.equalTo(enterBtn);
            make.left.equalTo(backView);
            make.height.mas_equalTo(30);
        }];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        [backView addSubview:rightLabel];
        rightLabel.textColor = RGBA(155, 155, 155, 1);
        rightLabel.font = [UIFont systemFontOfSize:10.f];
        rightLabel.text = @"(当前第1关/共3关)";
        rightLabel.textAlignment = NSTextAlignmentLeft;
        [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(enterBtn.mas_right).offset(3);
            make.centerY.equalTo(enterBtn);
            make.right.equalTo(backView);
            make.height.mas_equalTo(30);
        }];
        
        UILabel *line = [[UILabel alloc] init];
        [backView addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView);
            make.right.equalTo(backView);
            make.bottom.equalTo(backView).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
        
        BKLineChartView *lineChart = [[BKLineChartView alloc] initWithFrame:CGRectMake(10, 10, kWidth-20, 180*ScalesH)];
        [backView addSubview:lineChart];
        if ([_weakModel.AbiClass isEqualToString:@"A"]) { //听力能力
       
            lineChart.lineChartBackgroundColor =  RGBA(11, 178, 242, 1);
            lineChart.lineChartBackgroundColor2 = RGBA(35, 190, 250, 0)
        } else if ([_weakModel.AbiClass isEqualToString:@"B"]) { //发音
            
            lineChart.lineChartBackgroundColor =  RGBA(5, 174, 9, 1);
            lineChart.lineChartBackgroundColor2 = RGBA(5, 174, 9, 0);
       
        } else if ([_weakModel.AbiClass isEqualToString:@"C"]) { //朗读
            
            lineChart.lineChartBackgroundColor =  RGBA(5, 174, 9, 1);
            lineChart.lineChartBackgroundColor2 = RGBA(5, 174, 9, 0);
        }
        lineChart.Xarr = [ZDTimeString componentsSeparatedByString:@","];
        lineChart.Yarr = [ZDScoreString componentsSeparatedByString:@","];
        
        UIImageView *printImageView = [[UIImageView alloc] init];
        [backView addSubview:printImageView];
        printImageView.image = [UIImage imageNamed:@"能力较弱3"];
        [printImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(backView).offset(-22);
            make.top.equalTo(backView).offset(20);
            make.size.mas_equalTo(CGSizeMake(50, 41));
        }];
    }
}

- (void)pushAction:(UIButton*)sender {
    self.pushBlock();
}


@end
